package cn.lili.controller.user;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.permission.entity.dos.Department;
import cn.lili.modules.permission.entity.dto.DepartmentEditDTO;
import cn.lili.modules.permission.entity.dto.DepartmentSearchParams;
import cn.lili.modules.permission.entity.vo.DepartmentVO;
import cn.lili.modules.permission.integration.PermissionIntegrationService;
import cn.lili.modules.permission.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 部门管理接口
 *
 * @author Chopper
 * @since 2020/11/22 12:06
 */
@RestController
@Api(tags = "部门管理接口")
@RequestMapping("/user/department")
@RequiredArgsConstructor
public class DepartmentController {
    private final DepartmentService departmentService;


    private final PermissionIntegrationService permissionIntegrationService;

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查看部门详情")
    public ResultMessage<Department> get(@PathVariable String id) {
        Department department = departmentService.getById(id);
        return ResultUtil.data(department);
    }

    @GetMapping
    @ApiOperation(value = "获取树状结构")
    public ResultMessage<List<DepartmentVO>> tree(DepartmentSearchParams departmentSearchParams) {
        return ResultUtil.data(departmentService.tree(departmentSearchParams));
    }

    @PostMapping
    @ApiOperation(value = "新增部门")
    public ResultMessage<Department> save(Department department) {
        departmentService.saveDepartment(department);
        return ResultUtil.data(department);
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新部门")
    public ResultMessage<Object> update(@PathVariable String id, @RequestBody DepartmentEditDTO departmentEditDTO) {
        departmentEditDTO.setId(id);
        departmentService.updateDepartment(departmentEditDTO);
        return ResultUtil.success();
    }

    @DeleteMapping(value = "/{ids}")
    @ApiOperation(value = "删除部门")
    public ResultMessage<Object> delAllByIds(@PathVariable List<String> ids) {
        permissionIntegrationService.deleteDepartments(ids);
        return ResultUtil.success();
    }


    @GetMapping(value = "/tree/{scene}/{extendId}")
    @ApiOperation(value = "获取树状结构")
    public ResultMessage<List<DepartmentVO>> tree(@PathVariable String scene, @PathVariable String extendId) {
        return ResultUtil.data(departmentService.tree(scene, extendId));
    }
}
